package com.inspur.dgov.policy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.dgov.common.tool.api.R;
import com.inspur.dgov.policy.entity.InfoPolicy;
import com.inspur.dgov.policy.entity.PolicySubjectRelation;
import com.inspur.dgov.policy.service.EsPolicyService;
import com.inspur.dgov.policy.service.PolicySubjectRelationService;
import com.inspur.dgov.policy.service.PolicySubjectService;
import com.inspur.dgov.policy.vo.PolicySubjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 政策专题配置控制器
 *
 * @Author chiyupei
 * @Date 20/3/24 下午4:14
 * @Version 1.0
 * @Description 政策专题配置控制器
 */
@RestController
@RequestMapping("/subject")
@Api(value = "政策专题配置", tags = "政策专题配置")
public class PolicySubjectController {

    @Autowired
    private PolicySubjectService policySubjectService;

    @Autowired
    private PolicySubjectRelationService policySubjectRelationService;

    @Autowired
    private EsPolicyService esPolicyService;

    @Value("${es.push.flag}")
    private boolean esPushFlag;

    /**
     * 政策专题分页列表
     *
     * @param subjectId
     * @param query
     * @param page
     * @return
     */
    @PostMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", value = "专题ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "query", value = "查询条件", paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "政策专题分页列表", notes = "传入专题ID或查询条件", position = 1)
    public R<IPage<PolicySubjectVO>> page(@RequestParam(value="subjectId", required = false) String subjectId,
                                          @RequestParam(value="query", required = false) String query, Page page) {
        Map<String, Object> params = new HashMap<>();
        params.put("query", query);

        if (StrUtil.isNotBlank(subjectId)) {
            params.put("subjectId", subjectId);
        }
        IPage<PolicySubjectVO> pages = this.policySubjectService.selectPolicySubjectPage(page, params);
        return R.data(pages);
    }

    /**
     * 政策选择列表
     */
    @GetMapping("/selectPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", value = "专题ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "basicTitle", value = "政策标题", paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "政策选择列表", notes = "传入专题ID和政策标题", position = 2)
    public R<IPage<InfoPolicy>> selectPage(@ApiIgnore @RequestParam Map<String, Object> query, Page page) {
        IPage<InfoPolicy> pages = this.policySubjectService.selectPolicySelectPage(page, query);
        return R.data(pages);
    }

    /**
     * 为专题添加选择的政策
     */
    @PostMapping("/selectSave")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "policyIds", value = "政策ID集合", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subjectId", value = "专题ID", paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "添加某一专题的相关政策", notes = "传入政策ID集合和专题ID", position = 3)
    public R selectSave(@RequestParam(value="policyIds", required = true) String policyIds,
                        @RequestParam(value="subjectId", required = true) String subjectId) {
        if (StrUtil.hasEmpty(policyIds, subjectId)) {
            return R.fail("接口调用不合法");
        }
        List<PolicySubjectRelation> list = new ArrayList<>();
        String[] ids = policyIds.split(",");
        for (int i = 0; i < ids.length; i++) {
            if (StrUtil.isNotEmpty(ids[i])) {
                PolicySubjectRelation relation = new PolicySubjectRelation();
                relation.setPolicyId(ids[i]);
                relation.setSubjectId(subjectId);
                list.add(relation);
            }
        }
        boolean res = this.policySubjectRelationService.saveBatch(list);
        if (esPushFlag) {
            //循环更新专题到ES库
            this.esPolicyService.updateEsPolicySubject(list);
        }
        return R.status(res);
    }

    /**
     * 删除政策专题设置
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除政策专题设置", notes = "传入政策专题List", position = 4)
    public R remove(@RequestBody List<PolicySubjectRelation> list) {
        //删除政策专题关系
        for (int i = 0; i < list.size(); i++) {
            PolicySubjectRelation relation = list.get(i);
            UpdateWrapper<PolicySubjectRelation> updateWrapper = new UpdateWrapper();
            updateWrapper.lambda().eq(PolicySubjectRelation::getPolicyId, relation.getPolicyId())
                    .eq(PolicySubjectRelation::getSubjectId, relation.getSubjectId());
            this.policySubjectRelationService.remove(updateWrapper);
        }

        if (esPushFlag) {
            //循环更新专题到ES库
            this.esPolicyService.updateEsPolicySubject(list);
        }
        return R.status(true);
    }
}
